﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.ComMethod;
using LibrarySolution.DAO.基础;
using LibrarySolution.Model.基础;

namespace LibrarySolution.Frm
{
    public partial class FrmJsjl : Form
    {
        #region 属性
        JSRecord jsRecord;
        JHBkDao jhbkDao = new JHBkDao();
        private static FrmJsjl Instance;
        #endregion

        #region 构造器
        public FrmJsjl()
        {
            InitializeComponent();
            BindingData();
            this.RbtnAll.Checked = true;
        }
        #endregion

        #region 事件
        public static FrmJsjl GetInstance()
        {
            if (Instance == null || Instance.IsDisposed)
                Instance = new FrmJsjl();
            Instance.MdiParent = GlobalVar.frmMain;
            GlobalVar.frmMain.panel1.Controls.Add(Instance);
            Instance.BringToFront();
            return Instance;
        }

        private void TxtQueryByYhNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            String yhNo;
            if (e.KeyChar ==13)
            {
                yhNo = TxtQueryByYhNo.Text.Trim();
                jsRecord = new JSRecord();
                jsRecord.js_yhno = yhNo;
                if (RbtnWh.Checked == true)
                    jsRecord.js_state = "false";
                if (RbtnYh.Checked == true)
                    jsRecord.js_state = "true";
                dataGridView1.DataSource = jhbkDao.getTsjl(jsRecord);
                dataGridView1.Columns["图书编码"].Visible = false;
                dataGridView1.Refresh();
                TxtQueryByYhNo.Text = "";
            }
        }

        private void TxtQueryByTsNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            String tsNo;
            if (e.KeyChar == 13)
            {
                tsNo = TxtQueryByTsNo.Text.Trim();
                jsRecord = new JSRecord();
                jsRecord.js_tsbh = tsNo;
                if (RbtnWh.Checked == true)
                    jsRecord.js_state = "false";
                if (RbtnYh.Checked == true)
                    jsRecord.js_state = "true";
                dataGridView1.DataSource = jhbkDao.getTsjl(jsRecord);
                dataGridView1.Columns["图书编码"].Visible = false;
                dataGridView1.Refresh();
                TxtQueryByTsNo.Text = "";
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string value = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
            if (e.ColumnIndex == 0 && value == bool.FalseString)
            {
                if (MessageBox.Show("确定用户已还此书?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
                {
                    jsRecord = new JSRecord();
                    jsRecord.js_tsbm = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["图书编码"].Value.ToString();
                    jsRecord.js_code = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
                    jsRecord.js_tsbh = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString();
                    jsRecord.js_yhno = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4].Value.ToString();
                    jsRecord.js_shrq = DateTime.Now.ToString("yyyy-MM-dd");
                    jhbkDao.changeJs(jsRecord);
                    BindingData();
                }
            }
        }

        private void TxtQueryByTsmc_KeyPress(object sender, KeyPressEventArgs e)
        {
            String Tsmc;
            if (e.KeyChar == 13)
            {
                Tsmc = TxtQueryByTsmc.Text.Trim();
                jsRecord = new JSRecord();
                jsRecord.js_tsmc = Tsmc;
                if (RbtnWh.Checked == true)
                    jsRecord.js_state = "false";
                if (RbtnYh.Checked == true)
                    jsRecord.js_state = "true";
                dataGridView1.DataSource = jhbkDao.getTsjl(jsRecord);
                dataGridView1.Columns["图书编码"].Visible = false;
                dataGridView1.Refresh();
                TxtQueryByTsmc.Text = "";
            }
        }

        private void RbtnWh_CheckedChanged(object sender, EventArgs e)
        {
            BindingData();
        }

        private void RbtnYh_CheckedChanged(object sender, EventArgs e)
        {
            BindingData();
        }

        private void RbtnAll_CheckedChanged(object sender, EventArgs e)
        {
            BindingData();
        }
        #endregion

        #region 方法
        void BindingData()
        {
            jsRecord = new JSRecord();
            if (RbtnWh.Checked == true)
                jsRecord.js_state = "false";
            if (RbtnYh.Checked == true)
                jsRecord.js_state = "true";
            dataGridView1.DataSource = jhbkDao.getTsjl(jsRecord);
            dataGridView1.Columns["图书编码"].Visible = false;
            dataGridView1.Columns["是否扣费"].Visible = false;
            dataGridView1.Refresh();
        }
        #endregion
    }
}
